/***************************************************************************
 * 
 * Copyright (c) 2012 Baidu.com, Inc. All Rights Reserved
 * common.h,v 1.0 2012-08-17 11:30 yelu01 
 * 
 **************************************************************************/
 
 
 
/**
 * @file type_cast.h
 * @author yelu01(yelu01@baidu.com)
 * @date 2012-08-17 11:30
 * @version 1.0 
 * @brief macros to do type cast, output err info on failed.
 *  
 **/

#pragma once

#include "log.h"

#define ATOI(_result_, _str_, _err_process_) \
{\
    char* tmp_ = NULL; \
    if (NULL == _str_) \
    {\
        _err_process_; \
    }\
    _result_ = (int)strtol(_str_, &tmp_, 10); \
    if ('\0' == *_str_ || NULL == tmp_ \
      || ('\0' != *tmp_ && '\n' != *tmp_ && '\t' != *tmp_))\
    {\
        LOG(COMLOG_WARNING, "fail to convert %s to int", _str_); \
        _err_process_;\
    }\
}

#define ATOL(_result_, _str_, _err_process_) \
{\
    char* tmp_ = NULL; \
    if (NULL == _str_) \
    {\
        _err_process_; \
    }\
    _result_ = (long long)strtoul(_str_, &tmp_, 10); \
    if ('\0' == *_str_ || NULL == tmp_ \
      || ('\0' != *tmp_ && '\n' != *tmp_ && '\t' != *tmp_))\
    {\
        LOG(COMLOG_WARNING, "fail to convert %s to long long", _str_); \
        _err_process_;\
    }\
}

#define ATOU(_result_, _str_, _err_process_) \
{\
    char* tmp_ = NULL; \
    if (NULL == _str_) \
    {\
        _err_process_; \
    }\
    if (_str_[0] == '-') \
    { \
        LOG(COMLOG_WARNING, "%s is invalid unsigned int", _str_); \
        _err_process_; \
    } \
    _result_ = (unsigned int)strtoul(_str_, &tmp_, 10); \
    if ('\0' == *_str_ || NULL == tmp_ \
      || ('\0' != *tmp_ && '\n' != *tmp_ && '\t' != *tmp_))\
    {\
        LOG(COMLOG_WARNING, "fail to convert %s to unsigned int", _str_); \
        _err_process_;\
    }\
}

#define ATOULL(_result_, _str_, _err_process_) \
{\
    char* tmp_ = NULL; \
    if (NULL == _str_) \
    {\
        _err_process_; \
    }\
    if (_str_[0] == '-') \
    { \
        LOG(COMLOG_WARNING, "%s is invalid unsigned long long int", _str_); \
        _err_process_; \
    } \
    _result_ = (unsigned long long int)strtoull(_str_, &tmp_, 10); \
    if ('\0' == *_str_ || NULL == tmp_ \
      || ('\0' != *tmp_ && '\n' != *tmp_ && '\t' != *tmp_))\
    {\
        LOG(COMLOG_WARNING, "fail to convert %s to unsigned long long int", _str_); \
        _err_process_;\
    }\
}

#define ATOF(_result_, _str_, _err_process_) \
{\
    char* tmp_ = NULL; \
    if (NULL == _str_) \
    {\
        _err_process_; \
    }\
    _result_ = (float)strtod(_str_, &tmp_); \
    if ('\0' == *_str_ || NULL == tmp_ \
      || ('\0' != *tmp_ && '\n' != *tmp_ && '\t' != *tmp_))\
    {\
        LOG(COMLOG_WARNING, "fail to convert %s to double", _str_); \
        _err_process_;\
    }\
}

#define ATOD(_result_, _str_, _err_process_) \
{\
    char* tmp_ = NULL; \
    if (NULL == _str_) \
    {\
        _err_process_; \
    }\
    _result_ = (double)strtod(_str_, &tmp_); \
    if ('\0' == *_str_ || NULL == tmp_ \
      || ('\0' != *tmp_ && '\n' != *tmp_ && '\t' != *tmp_))\
    {\
        LOG(COMLOG_WARNING, "fail to convert %s to double", _str_); \
        _err_process_;\
    }\
}



